package com.swt.gap.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.swt.gap.common.PageResult;
import com.swt.gap.dao.TaskSubscribeDataDao;
import com.swt.gap.model.dto.SubscribeFileDTO;
import com.swt.gap.model.entity.TaskSubscribeData;
import com.swt.gap.model.vo.TaskSubscribeDataVO;
import com.swt.gap.service.TaskSubscribeDataService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class TaskSubscribeDataServiceImpl implements TaskSubscribeDataService {

    private final TaskSubscribeDataDao taskSubscribeDataDao;

    public TaskSubscribeDataServiceImpl(TaskSubscribeDataDao taskSubscribeDataDao) {
        this.taskSubscribeDataDao = taskSubscribeDataDao;
    }


    @Override
    public List<TaskSubscribeDataVO> listTaskSubscribeDataUnread(Integer[] taskIds, Integer status) {
        //如果未传状态参数，默认给1 即未读
        status = Optional.ofNullable(status).orElse(1);
        List<TaskSubscribeDataVO> retValue = new ArrayList<>();
        for (Integer taskId : taskIds) {
            List<TaskSubscribeData> taskSubscribeData = taskSubscribeDataDao.selectByTaskIdAndStatus(taskId, status);
            List<Integer> subscribeIds = new ArrayList<>();
            List<SubscribeFileDTO> subscribeFiles = new ArrayList<>();
            taskSubscribeData.forEach(data -> {
                subscribeIds.add(data.getSubscribeId());
                subscribeFiles.add(JSON.parseObject(data.getFileContent(), SubscribeFileDTO.class));
            });
            TaskSubscribeDataVO taskSubscribe = TaskSubscribeDataVO.builder()
                    .taskId(taskId)
                    .subscribeIds(subscribeIds)
                    .count(subscribeIds.size())
                    .subscribeFiles(subscribeFiles)
                    .build();
            retValue.add(taskSubscribe);
        }
        return retValue;
    }

    @Override
    public PageResult<SubscribeFileDTO> listTaskSubscribeData(Integer taskId, Integer status, String fileName,
                                                              Integer pageNum, Integer pageSize) {
        if (null == pageNum || null == pageSize) {
            pageNum = 1;
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);

        Page<SubscribeFileDTO> pageRes = taskSubscribeDataDao.selectByTaskIdAndStatusAndFileName(taskId, status, fileName);
        return new PageResult<>(pageRes);
    }


    @Override
    public Integer updateStatusReed(Integer[] subscribeIds) {

        return taskSubscribeDataDao.updateStatusReed(subscribeIds);
    }

    @Override
    public Integer deleteTaskSubscribeData(Integer[] subscribeIds) {
        int retValue = 0;
        for(Integer subscribeId: subscribeIds) {
            TaskSubscribeData taskSubscribeData = TaskSubscribeData.builder()
                    .subscribeId(subscribeId)
                    .deleted(true)
                    .build();
            retValue += taskSubscribeDataDao.updateByPrimaryKeySelective(taskSubscribeData);
        }
        return retValue;
    }
}
